const message = require("../class/message");
const { connectMysql } = require("./mysqlconnect")

function insertMysql(message) {
    const sql = "insert into message (name,message,create_time) values (?,?,?)";
    const data = [message.name, message.message,message.create_time]
    return connectMysql(sql,data)
}


function list() {
    const sql = "select * from message order by id desc";
    const data = []
    return connectMysql(sql, data)
}

// 更新
function updata(message) {
    const sql = "update message set name=?,message=? where id = ?"
    const data = [message.name, message.message, message.id]
    return connectMysql(sql, data)
}

// 查询列表
function selectList({ searchName = '', searchContent = '', pageNum = 1, pageSize = 8 }) {
    const sql = "select * from message where name like ? and message like ? order by id desc limit ?,? "
    const data = ["%" + searchName + "%", "%" + searchContent + "%", (pageNum - 1) * pageSize, pageSize]
    return connectMysql(sql, data)
}
// 个数统计
function count({ searchName = "", searchContent = "" }) {
    const sql = "select count(*) total from message where name like ? and message like ?"
    const data = ["%" + searchName + "%", "%" + searchContent + "%"]
    return connectMysql(sql, data)
}

// 删除列表
function deleteItem(id) {
    const sql = "delete from message where id = ?"
    const data = [id]
    return connectMysql(sql, data)
}
module.exports = {
    insertMysql,
    list,
    updata,
    selectList,
    count,
    deleteItem
}